package com.mes.basicdata.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.mes.basicdata.dao.iface.IOperationDao;
import com.mes.basicdata.dao.iface.IRoutingOperationDao;
import com.mes.basicdata.domain.Operation;
import com.mes.basicdata.manager.iface.IOperationManager;
import com.mes.common.domain.BasicModel;
import com.mes.common.service.BasicService;
import com.mes.common.util.SysUtil;
@Service
public class OperationService extends BasicService<Operation> {
	
	@Autowired IOperationDao operationDao;
	
	@Autowired IOperationManager operationManager;
	
	@Autowired IRoutingOperationDao routingOperationDao;
	
    @DataProvider
    public List<Operation> getOperationsByRoutingUid(String routingUid){

		try {
			List<Operation> list = operationDao.getOperationListByRoutingUid(routingUid);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

    }
    
	@DataProvider
	public List<Operation> getOperationByUid(String operationUid){
		try {
			Operation operation = operationDao.getById(operationUid);
			List<Operation> list = new ArrayList<Operation>();
			list.add(operation);
			
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@DataProvider
    public void getOperationsByConditions(Page<Operation> page, Map<String, Object> params){
    	if (params != null && !params.equals("") ){
    		try {
    			operationDao.getOperationsByConditions(page, params);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}else {
			try {
				this.getAll(page);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    }
	@DataProvider
    public List<Operation> getOperationStepByParentOpUid(Map<String, Object> params){
    	if (params != null && !params.equals("") ){
    		try {
    			return operationDao.getOperationStepByParentOpUid(params);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
		return null;
    }

	@SuppressWarnings("all")
	@DataResolver
	public void saveOperations(Map<String, Collection<Operation>> dataItems,Map<String,Object> parameter) throws Exception{
		String routingUid =(String) parameter.get("routingUid");
		for (Iterator iterator = dataItems.values().iterator(); iterator.hasNext();) {
			Collection<Operation> operations = (Collection)iterator.next();
			for (Operation operation : operations) {
				if (null!=operation) {
					switch (EntityUtils.getState(operation)) {
					case MODIFIED:
						operation.setObjectState(BasicModel.OBJECTSTATE_TOBEUPDATED);
						break;
					case DELETED:
						operation.setObjectState(BasicModel.OBJECTSTATE_TOBEDELETED);
						break;
					case NEW:
						operation.setObjectState(BasicModel.OBJECTSTATE_TOBEADDED);
						operation.setUuid(SysUtil.getUUID());
						break;
					case NONE:
						operation.setObjectState(BasicModel.OBJECTSTATE_NONE);
						break;
					default:
						break;
					}
				}
			}
			
			operationManager.saveOperations(operations,routingUid);
		}
	}
}
